import 'dart:async';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/base/base_entity.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/all_info.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/constans/route_constants.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';

import 'action.dart';
import 'entity/login_entity.dart';
import 'state.dart';

import 'package:rammus/rammus.dart' as rammus;
Effect<LoginState> buildEffect() {
  return combineEffects(<Object, Effect<LoginState>>{
    LoginAction.getCodeCLick: _onGetCode,
    LoginAction.login: _login,
  });
}

void _onGetCode(Action action, Context<LoginState> ctx) {
  if(ctx.state.mobile.length!=11){
    showToast('请输入手机号');
    return;
  }
  Map<String, String> params = Map();
  params["mobile"] = ctx.state.mobile;
  params["type"] = "1";
  DioApi.instance.asyncRequestNetwork<BaseEntity>(
      Method.post,
      HttpApi.GET_CODE,ctx.context,
      params: params, onStart: () {
    println('请求开始');
    EasyLoading.show(status: 'loading...');
  }, onSuccess: (dynamic t) async {
    //请求成功
    ctx.state
      ..countdownTime = 60
      ..textColor = AppColors.smallTextColor;
    ctx.state
      ..countdownTimer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
        if (ctx.state.countdownTime < 1) {
          ctx.state.countdownTimer.cancel();
          ctx.state
            ..countdownTime = 0
            ..countdownString = "发送验证码"
            ..textColor = AppColors.cantClickTextColor;
        } else {
          ctx.state..countdownTime = ctx.state.countdownTime - 1;
          ctx.state..countdownString = "${ctx.state.countdownTime}s后重发";
        }
        ctx.dispatch(LoginActionCreator.onAction());
      });
  }, onError: (String msg) {
    println('请求报错 msg:$msg');
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}

void _login(Action action, Context<LoginState> ctx){
  switch(ctx.state.loginWayType){
    case Constants.LOGIN_PWD_WAY:
      loginWithPwd(ctx);
      break;
    case Constants.LOGIN_CODE_WAY:
      loginWithCode(ctx);
      break;
  }
}

void loginWithCode(Context<LoginState> ctx){
  if(ctx.state.mobile.length!=11){
    showToast('请输入手机号');
    return;
  }
  if(ctx.state.code.isEmpty){
    showToast('请输入验证码');
    return;
  }
  Map<String, String> params = Map();
  params["mobile"] = ctx.state.mobile;
  params["sms_code"] = ctx.state.code;
  DioApi.instance.asyncRequestNetwork<BaseEntity>(
      Method.post,
      HttpApi.LOGIN_WITH_CODE,ctx.context,
      params: params, onStart: () {
    println('请求开始');
    EasyLoading.show(status: 'loading...');
  }, onSuccess: (dynamic t) async {
    LoginEntity loginEntity = EntityFactory.generateOBJ(t);
  }, onError: (String msg) {
    println('请求报错 msg:$msg');
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}
void loginWithPwd(Context<LoginState> ctx){
  if(ctx.state.mobile.length!=11){
    showToast('请输入手机号');
    return;
  }
  if(ctx.state.pwd.isEmpty){
    showToast('请输入密码');
    return;
  }
  Map<String, String> params = Map();
  params["mobile"] = ctx.state.mobile;
  params["password"] =ctx.state.pwd;
  DioApi.instance.asyncRequestNetwork<LoginEntity>(
      Method.post,
      HttpApi.LOGIN_WITH_PWD,ctx.context,
      params: params, onStart: () {
    println('请求开始');
    EasyLoading.show(status: 'loading...');
  }, onSuccess: (dynamic t) async {
    LoginEntity loginEntity = EntityFactory.generateOBJ(t);
    AllInfo.instance.setToken(loginEntity.token);
    rammus.bindAccount(loginEntity.token);
    Navigator.of(ctx.context).pushNamedAndRemoveUntil(RouteConstants.INDEX, (route) => false);
  }, onError: (String msg) {
    println('请求报错 msg:$msg');
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}